﻿using System;

using Autofac;

using Cookbook.Services.Core.Domain;
using Cookbook.Services.Core.Persistency;

using Cooking.Services.Specs.Builders;
using Cooking.Services.Specs.Support;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Cooking.Services.Specs.Persistency
{
    [TestClass]
    public class ProductRepositorySpecs
    {
        [TestMethod]
        public void When_removing_a_product_that_is_used_in_a_recipe_it_should_throw()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var containerBuilder = new ContainerBuilder();
            containerBuilder.RegisterModule(new CookbookUnitOfWorkModule("Cookbook"));
            var container = containerBuilder.Build();

            using (var uow = container.Resolve<Func<CookbookUnitOfWork>>()())
            {
                Product mayonaise = new ProductBuilder().Build();
                uow.Products.Add(mayonaise);

                Recipe recipe = new RecipeBuilder()
                    .WithIngredient(200, mayonaise)
                    .Build();

                uow.Recipes.Add(recipe);
                uow.SubmitChanges();
                
                //-----------------------------------------------------------------------------------------------------------
                // Act
                //-----------------------------------------------------------------------------------------------------------
                Action action = () =>
                {
                    uow.Products.Remove(mayonaise);
                    uow.SubmitChanges();
                };

                //-----------------------------------------------------------------------------------------------------------
                // Assert
                //-----------------------------------------------------------------------------------------------------------
                action.ShouldViolate(CookbookRule.ProductCannotBeRemovedIfStillUsed);
            }
        }

    }
}
